package tool;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import exception.PathVariableSuffixNotNumbersException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/* 
 * 所有组件抛出的异常由DispatcherServlet交给异常处理器HandlerExceptionResolver处理
 */
@Controller
public class PathVariableSuffixNotNumbersExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
            Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView("exception");
        if (e instanceof PathVariableSuffixNotNumbersException) {
            modelAndView.addObject("exception", e.getMessage());
        } else {
            e = new Exception("捕获了一个预料之外的异常");
            modelAndView.addObject("exception", e.getMessage());
        }
        return modelAndView;
    }
}
